package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class SpringRabbitListener {

    /**
     * BasicQueue, 监听simple.queue队列的消息
     * @param msg 消息
     */
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) {
//        System.out.println("spring 消费者接收到消息：【" + msg + "】");
//    }

    /**
     * WorkQueue, 监听work.queue队列的消息
     *
     * @param msg 队列消息
     */
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue1(String msg) throws InterruptedException {
//        log.info("消费者1接收到消息：【" + msg + "】");
//        Thread.sleep(20);
//    }
//
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue2(String msg) throws InterruptedException {
//        log.error("消费者2接收到消息：【" + msg + "】");
//        Thread.sleep(200);
//    }

    /**
     * Publish/Subscribe, 监听fanoutExchange交换机的消息
     *
     * @param msg 队列消息
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        log.info("消费者1接收到fanout.queue1消息：【" + msg + "】");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        log.info("消费者2接收到fanout.queue2消息：【" + msg + "】");
    }

    /**
     * Routing, 监听directExchange交换机的消息
     *
     * @param msg 队列消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void listenDirectQueue1(String msg) {
        log.info("消费者1接收到direct.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenDirectQueue2(String msg) {
        log.error("消费者2接收到direct.queue2的消息：【" + msg + "】");
    }

    /**
     * Topic, 监听topicExchange交换机的消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void listenTopicQueue1(String msg) {
        log.info("消费者1接收到topic.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void listenTopicQueue2(String msg) {
        log.error("消费者2接收到topic.queue2的消息：【" + msg + "】");
    }

    /**
     * 接收对象消息
     *
     * @param msg 消息
     */
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(Object msg) {
        log.info("消费者接收到对象消息：【" + msg + "】");
    }
}